package Dtss;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Scanner;

public class util {

    public static Team team_1 = new Team(3, 2, 1);

    public static ArrayList<Employee> employeeArrayList = createEmployee();

    public static Scanner scan = new Scanner(System.in);

    public static void menu() {
        System.out.println("--------------------开发团队调度软件--------------------\n");
        System.out.println("ID\t\t姓名  \t\t年龄\t\t工资    \t\t职位  \t\t状态");
        for (Employee employee : employeeArrayList) {
            System.out.printf("%-2d\t\t%-3s\t\t%-3d\t\t%2.2f\t\t%-3s\t\t%-4.4s\n", employee.getId(), employee.getName(), employee.getAge(), employee.getSalary(), employee.getPosition(), employee.getStatus());
        }
        System.out.println("-----------------------------------------------------");
        System.out.print("1-团队列表");
        System.out.print("2-增加团队成员");
        System.out.print("3-删除团队成员");
        System.out.print("4-退   出");
        System.out.print(" 请输入1-4:");
    }
    public static @NotNull ArrayList<Employee> createEmployee() {
        ArrayList<Employee> employees = new ArrayList<>();
        Employee employee1 = new Employee(1, "马云", (byte) 45, 2000.0, "程序员", "FREE");
        Employee employee2 = new Employee(2, "马化腾", (byte) 40, 4000.0, "架构师", "FREE");
        Employee employee3 = new Employee(3, "张三", (byte) 35, 8000.0, "程序员", "FREE");
        Employee employee4 = new Employee(4, "李四", (byte) 36, 6000.0, "架构师", "FREE");
        Employee employee5 = new Employee(5, "王五", (byte) 37, 7700.0, "程序员", "FREE");
        Employee employee6 = new Employee(6, "赵六", (byte) 38, 5500.0, "程序员", "FREE");
        Employee employee7 = new Employee(7, "钱七", (byte) 39, 4400.0, "设计师", "FREE");
        Employee employee8 = new Employee(8, "孙八", (byte) 34, 3300.0, "设计师", "FREE");
        Employee employee9 = new Employee(9, "曹操", (byte) 33, 8800.0, "程序员", "FREE");
        Employee employee10 = new Employee(10, "典韦", (byte) 32, 9900.0, "设计师", "FREE");
        Employee employee11 = new Employee(11, "鲁迅", (byte) 31, 3900.0, "程序员", "FREE");
        Employee employee12 = new Employee(12, "张飞", (byte) 30, 5900.0, "程序员", "FREE");
        employees.add(employee1);
        employees.add(employee2);
        employees.add(employee3);
        employees.add(employee4);
        employees.add(employee5);
        employees.add(employee6);
        employees.add(employee7);
        employees.add(employee8);
        employees.add(employee9);
        employees.add(employee10);
        employees.add(employee11);
        employees.add(employee12);
        return employees;
    }

    public static char readFlag() {
        return scan.next().charAt(0);
    }

    public static char confirm() {
        return scan.next().charAt(0);
    }


    public static void showTeam() {
        System.out.println("--------------------团队成员--------------------");
        System.out.println("ID/TID\t\t姓名  ");
        for (TeamMember employee: team_1.teamMemberArrayList) {
            System.out.printf("%-2d/%-3d\t\t%-3s\n", employee.getId(), employee.getTID(), employee.getName());
        }
        System.out.println("----------------------------------------------");
    }

    public static void addMember() {
        System.out.print("请输入你要添加的队员的id:");
        int employeeId = getEmployeeId();
        if (employeeId > 0) {
            for (Employee employee : employeeArrayList) {
                if (employeeId == employee.getId() && isAdd()) {
                    team_1.teamMemberArrayList.add(new TeamMember(employee.getId(), employee.getName(), employee.getAge(), employee.getSalary(), employee.getPosition(), "BUSY", team_1.getNextId()));
                    employee.setStatus("BUSY");
                    team_1.setNextId();
                    if (employee.getPosition().equals("程序员")) {
                        team_1.setProgrammerNumber(true);
                    } else if (employee.getPosition().equals("架构师")) {
                        team_1.setArchitectNumber(true);
                    } else {
                        team_1.setDesignerNumber(true);
                    }
                }
            }
        }
    }

    private static boolean isAdd() {
        if (team_1.getProgrammerNumber() < 1) {
            return false;
        }
        if (team_1.getArchitectNumber() != 1) {
            return false;
        }
        return team_1.getDesignerNumber() >= 1;
    }

    public static void deleteMember() {
        System.out.print("请输入你要删除队员的TID:");
        int TID = getEmployeeId();
        System.out.print("请确认删除TID为"+TID+"的队员(y/n):");
        char flag = confirm();
        if (flag == 'y') {
            for (TeamMember tempMember : team_1.teamMemberArrayList) {
                if (tempMember.getTID() == TID) {
                    if (tempMember.getPosition().equals("程序员")) {
                        team_1.setProgrammerNumber(false);
                    } else if (tempMember.getPosition().equals("架构师")) {
                        team_1.setArchitectNumber(false);
                    } else {
                        team_1.setDesignerNumber(false);
                    }

                    for (Employee tempEmployee : employeeArrayList) {
                        if (tempEmployee.getId().equals(tempMember.getId())) {
                            tempEmployee.setStatus("FREE");
                        }
                        break;
                    }

                    team_1.teamMemberArrayList.remove(tempMember);
                    System.out.println("成功删除TID为" + TID + "的队员...");
                    return;
                }
            }
        } else {
            System.out.println("删除操作取消...");
        }
    }

    public static int getEmployeeId() {
        int memberId = 0;
        while (true) {
            try {
                memberId = scan.nextInt();
            } catch (Exception e) {
                e.getStackTrace();
                continue;
            }
            if (memberId == -1 || memberId > 0) {
                return memberId;
            }
        }
    }

}
